package net.yla.smartbus.module.ba.vehicletype.serice.impl;

import net.yla.smartbus.module.ba.vehicletype.dto.BrandDto;
import net.yla.smartbus.module.ba.vehicletype.dto.BrandTree;
import net.yla.smartbus.module.ba.vehicletype.entity.BaBrandsDo;
import net.yla.smartbus.module.ba.vehicletype.entity.BaVehicleModelsDo;
import net.yla.smartbus.module.ba.vehicletype.form.BrandForm;
import net.yla.smartbus.module.ba.vehicletype.mapper.BrandMapper;
import net.yla.smartbus.module.ba.vehicletype.mapper.VehicleTypeMapper;
import net.yla.smartbus.module.ba.vehicletype.serice.BrandService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 默苍离
 * @date 2021/6/24 9:35
 * <p>
 * ******************************************
 * @annotation:
 */
@Service
public class BrandServiceImpl implements BrandService {

    private final VehicleTypeMapper vehicleTypeMapper;

    private final BrandMapper mapper;

    public BrandServiceImpl(BrandMapper mapper, VehicleTypeMapper vehicleTypeMapper) {
        this.mapper = mapper;
        this.vehicleTypeMapper = vehicleTypeMapper;
    }


    /**
     * 获取所有品牌信息
     *
     * @return 品牌信息集合
     */
    @Override
    public List<BrandDto> getList() {
        List<BaBrandsDo> brandsList = mapper.getList();
        List<BrandDto> brands = new ArrayList<>();
        for (int i = 0; i < brandsList.size(); i++) {
            BaBrandsDo baBrands = brandsList.get(i);
            BrandDto brandDto = new BrandDto();
            brandDto.setId(baBrands.getBrandId());
            brandDto.setName(baBrands.getName());

            brands.add(brandDto);
        }
        return brands;
    }


    /**
     * 获取去品牌树
     *
     * @return
     */
    @Override
    public List<BrandTree> getBrandTree() {

        //获取所有品牌信息
        List<BaBrandsDo> brandsList = mapper.getList();
        List<BrandTree> brandTrees = new ArrayList<>();
        for (int i = 0; i < brandsList.size(); i++) {

            BrandTree brandTree = new BrandTree();
            BaBrandsDo baBrands = brandsList.get(i);

            brandTree.setValue(baBrands.getBrandId());
            brandTree.setLabel(baBrands.getName());

            //根据  品牌id 获取车辆类型信息
            List<BaVehicleModelsDo> vehicleType = vehicleTypeMapper.getVehicleType(baBrands.getBrandId());
            List<BrandTree> brandTreeChildren = new ArrayList<>();

            for (int j = 0; j < vehicleType.size(); j++) {
                BrandTree brandTree1 = new BrandTree();
                BaVehicleModelsDo vehicleModelsDo = vehicleType.get(j);
                brandTree1.setValue(vehicleModelsDo.getModelId());
                brandTree1.setLabel(vehicleModelsDo.getName());

                brandTreeChildren.add(brandTree1);
            }

            brandTree.setChildren(brandTreeChildren);
            brandTrees.add(brandTree);
        }

        return brandTrees;
    }


    /**
     * 添加品牌
     *
     * @param form
     * @return
     */
    @Override
    public Integer insert(BrandForm form) {
        Integer insert = mapper.insert(form);
        return insert;
    }


}
